home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13053 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  74 lines

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: array of ptrs to structures
  5. Date: Wed, 03 Apr 1996 21:13:33 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <31635A7D.408405C4@alcyone.com>
  8. References: <Pine.SOL.3.91-941213.960403155531.22205C-100000@altair.dur.ac.uk>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. Ed Wiles wrote:
  16. > [I'm a bit of a C novice, would appreciate your (combined) help, thanks!]
  17. > "struct person" contains three variables: lastname, firstname, phone
  18. > (all arrays of char).
  19. > Within main(), I've declared an array of pointers to "struct person":
  20. >    struct person *phone_book[6];
  21. > 1) How do I hard-code, for example, the third person in the array to be:
  22. >    lastname = "Adcock";  firstname = "Lucy";  phone = "(01623) 943086"; ?
  23. >    *phone_book[2] = {"Adcock", "Lucy", etc.} is not allowed, so do I have to
  24. >    assign each structure member individually?
  25. >    i.e. *phone_book[2]->lastname = "Adcock", etc.
  26.  
  27. You have to use the string functions in C.  Strings are not fundamental
  28. datatypes in C; strings are merely arrays of characters which follow the
  29. convention of being NUL-terminated.
  30.  
  31. >    (In fact, I really want to hard-code all the people in the array in one
  32. >     go: is there a way of doing this when I declare the variable phone_book,
  33. >     remembering that e.g. int odds[] = {1, 3, 5, 7} is allowed?)
  34.  
  35. You can do this with an initialization when you declare the array (but not if
  36. you declare an array of pointers, at least not easily in a way that will be
  37. useful to you).
  38.  
  39. It will look something like this:
  40.  
  41. struct person phoneBook[] = { { "Adcock", "Lucy", "(01623) 943086" }, 
  42.                               { "Doe",    "John", "415 555 1212" }, 
  43.                               /* and so on */ };
  44.  
  45. >    Also, the pointer phone_book[2] is uninitialised, so it could be the
  46. >    address of anywhere... How do I initialise a pointer, please? (It is
  47. >    NEW(pointer_name) in Modula-2, so there must be a C equivalent..?)
  48.  
  49. That's the essential point here.  Look into malloc and free.
  50.  
  51. Note that if you are merely interested in a fixed array of six structures,
  52. using dynamic allocation is probably a bad idea (unless you have a good reason
  53. for doing this).  Just declare them statically.
  54.  
  55. > 2) I want to pass the entire array to a function. Do I just pass it as
  56. >    phone_book? Should the function's corresponding formal parameter be
  57. >    struct person *anyname[] ?
  58. > Thanks a lot!
  59. > - Ed Wiles (e.d.wiles@dur.ac.uk)
  60.  
  61. -- 
  62. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  63. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  64. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  65. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  66. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  67.